package com.shuoyue.ycgk.ui.mine.mynote;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.ChapterType;
import com.shuoyue.ycgk.entity.PersonNote;
import com.shuoyue.ycgk.ui.mine.mynote.adapter.PersonNoteQuestionAdapter;
import com.shuoyue.ycgk.ui.mine.mynote.info.MyNoteInfoActivity;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MineNoteListActivity extends BaseMvpActivity<MyNoteContract.Presenter>
        implements MyNoteContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    String name;
    int sectionId;
    int like;
    PersonNoteQuestionAdapter personNoteQuestionAdapter;

    public static void start(Context context, int sectionId, int like, String name) {
        context.startActivity(new Intent(context, MineNoteListActivity.class)
                .putExtra("sectionId", sectionId)
                .putExtra("like", like)
                .putExtra("name", name));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_note_list;
    }

    @Override
    protected void initView() {
        like = getIntent().getIntExtra("like", -1);
        sectionId = getIntent().getIntExtra("sectionId", -1);


        name = getIntent().getStringExtra("name");
        pageTitle.setText(name);
        refreshlayout.setEnableLoadMore(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        recyclerView.addItemDecoration(new SpacesItemDecoration(1, false));

        personNoteQuestionAdapter = new PersonNoteQuestionAdapter(null, like);
        personNoteQuestionAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            PersonNote personNote = (PersonNote) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_praise:
                    mPresenter.praiseNote(personNote, personNote.getIsLike() == 1 ? 0 : 1);
                    break;
                case R.id.question_title:
                    MyNoteInfoActivity.start(mContext, personNote.getQuestionId());
                    break;
                case R.id.delete:
                    HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "删除", "确认删除此笔记？");
                    hintConfirmDialog.setOkListener(v -> {
                        mPresenter.deleteNote(personNote);
                        hintConfirmDialog.dismiss();
                    });
                    hintConfirmDialog.show();
                    break;
            }
        });
        recyclerView.setAdapter(personNoteQuestionAdapter);
        registEmptyView(personNoteQuestionAdapter);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getQuestions(sectionId, like);
            refreshLayout.finishRefresh(2000);
        });
    }

    @Override
    public void initData() {

        mPresenter = new MyNoteContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getQuestions(sectionId, like);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setTypeList(List<ChapterType> chapterSectionTypes) {

    }

    @Override
    public void setQuestions(List<PersonNote> noteList) {
        personNoteQuestionAdapter.resetData(noteList);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void praiseResult(PersonNote note, boolean isPraised) {
        note.setIsLike(isPraised ? 1 : 0);
        note.setLikeCount(note.getLikeCount() + (isPraised ? 1 : -1));
        personNoteQuestionAdapter.notifyDataSetChanged();
    }

    @Override
    public void deleteSuc(PersonNote questionNote) {
        personNoteQuestionAdapter.remove(personNoteQuestionAdapter.getData().indexOf(questionNote));
    }

}
